<!--
 * Tencent is pleased to support the open source community by making Tars available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>服务下线</title>
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/bootstrap_table.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!--[if lt IE 9]>
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../js/jquery.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/template.js"></script>
    <script src="../js/common.js"></script>
</head>
<body>
<h4 id="main_title">服务下线</h4>
<form class="form-horizontal" role="form">
    <div class="form-group">
        <div class="col-sm-1"><label for="app-name" class="control-label wp100">应用 <span class="text-danger">*</span></label></div>
        <div class="col-sm-2">
            <input type="text" name="application" id="app-name" class="form-control">
        </div>
        <div class="col-sm-1"><label for="server-name" class="control-label wp100">服务 <span class="text-danger">*</span></label></div>
        <div class="col-sm-2">
            <input type="text" name="server-name" id="server-name" class="form-control">
        </div>
        <div class="col-sm-1"><label for="node-name" class="control-label wp100">节点 <span class="text-danger">*</span></label></div>
        <div class="col-sm-2">
            <input type="text" name="node-name" id="node-name" class="form-control">
        </div>
        <div class="col-sm-1">
            <button type="button" class="btn btn-primary">查询</button>
        </div>
    </div>
</form>

<div id="table-box"></div>


<div id="msg" style="display: none"></div>



<script type="text/html" id="tpl">
    <table class="table table-hover table-bordered" id="obj-table">
        <thead>
        <tr>
            <th>应用</th>
            <th>服务</th>
            <th>节点</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {{each data as it}}
        <tr>
            <td>{{it.application}}</td>
            <td>{{it.server_name}}</td>
            <td>{{it.node_name}}</td>
            <td>
                {{if it.status=='active'}}
                <a href="#;" onclick="offline({{it.id}})">下线</a>
                {{/if}}
            </td>
        </tr>
        {{/each}}
        </tbody>
    </table>
</script>



<script>

    getServiceList();


    /**
     * 服务下线
     * @param id {Number}  服务ID
     */
    function offline(id) {
        var $msg = $('#msg');
        $msg.html('<div>您确定要下线吗？</div>');
        showDialog($msg,'服务下线',{
            '取消':function () {
                this.modal('hide');
            },
            '下线':function () {
                var self = this;
                showLoading();
                $.getJSON('/pages/server/api/offline.json?id='+id,function (data) {
                    removeLoading();
                    if(data.ret_code==200){
                        getServiceList();
                        self.modal('hide');
                    }else{
                        showErrorMsg($msg,'服务下线出错');
                    }
                });
            }
        });
    }

    /**
     * 获取服务列表
     * @param treeId {String} 树ID
     */
    function getServiceList(treeId) {
        showLoading();
        $.getJSON('/pages/server/api/server_list?tree_node_id='+treeId,function (data) {
            removeLoading();
            var htmlStr = '';
            if(data.ret_code==200 && !data.sub_code) {
                htmlStr = template('seviceTpl', data);
                $('#table-box').html(htmlStr);
                $('#table').table();
                $('.get_curr_status').each(function () {
                    var $self = $(this);
                    getServiceCurrStatus($self.attr('data-serviceid'),$self);
                });
            }else if(data.ret_code==200 && data.sub_code){
                $('#table-box').html('<p class="text-danger tc">参数错误</p>');
            }else{
                $('#table-box').html('<div class="alert alert-danger" role="alert">加载出错：'+data.err_msg+'</div>');
            }
        });
    }
</script>
</body>
</html>
